#|----------------------------------------------------------------------------|
#|                           TOPMake 2.1                                      |
#| Copyright (c) China UnionPay Co., Ltd.                                     |
#|    All Rights Reserved                                                     |
#|----------------------------------------------------------------------------|
#| FILE NAME    : mksstand.mak                                                |
#| DESCRIPTIONS : Makefile for all kind of platform, for c and pc file,       |
#|                for lib(dynamic & static) and execuatable object            |
#|----------------------------------------------------------------------------|
#
#|----------------------------------------------------------------------------|
#|                                                                            |
#| What's new in 2.1 by tyxiang 2011:                                         |
#|      (1)   Order .bnd files based on name prefix.                          |
#|      (2)   Support partial compiling and linking by using VPATH.           |
#|                                                                            |
#|----------------------------------------------------------------------------|

include $(MKHOME)/mak/common.mak

# environment variables must be define outside of makefile
#
# $MKHOME : home directory of application 
# $CCFLG  : compiler options for c source code
# $CXXFLG : compiler options for c++ source code
# $DLFLG  : link options for dynamic library
# $SLFLG  : link options for static library
# $EXFLG  : link options for excecuteable object
# $DBFLG  : debug mode options
# $OPFLG  : optimize mode options
# $DLFIX  : suffix of dynamic library
# $SLFIX  : suffix of static library

# $OBJS   : c object files list
# $LIBS   : libs required for executable object
# $DEFS   : define flag list
# $TARGET : target objetc name

# $TUXSVCS: tuxedo services name
# $TUXRSMN: tuxedo resource manager name

# $DBNM   : db name (for db2)

# Variables that need to be defined in building script:
# VPATH   : used to search for object files in partial building
# ROLL_FLG : used to mark rolling building


# commands
 
PC		=	proc
EC		=	esql
DB2		=	db2

JAVAC   =   javac
JAVAH   =   javah
RMIC    =   rmic

RM		=	rm -f
CP		=	cp
MV		=   mv
CD		=   cd
ECHO	=	@echo

# paths and flags

SYSINCP	=	/usr/include
SYSLIBP	=	/usr/lib
JINCP	=	$(JAVA_HOME)/include
JMDINCP	=	$(JAVA_HOME)/include/$(JAVAMD)

SRCP	=	$(MKHOME)/src
BINP	= 	$(MODULE_HOME)/bin
TUXBINP =   $(MODULE_HOME)/esb/bin
TUXCLIBINP = $(MODULE_HOME)/esb/bin
LIBP	=	$(MODULE_HOME)/lib

INCP    =   $(MODULE_HOME)/include
CLSP    =   $(BINP)/classes
JLIBP   =   $(JAVA_HOME)/lib
BNDDIR  =   $(MODULE_HOME)/bnd
OBJDIR  =   $(VPATH)

INCPS	=	$(EXT_INCP) -I$(SYSINCP) -I$(INCP) $(DBINCP) $(TUXINCP) -I$(JINCP) -I$(JMDINCP)
#add EXT_INCP for CUNITE by mgzhang 20060925
LIBPS	=	-L$(SYSLIBP) -L$(XLIBP) -L$(LIBP) $(DBLIBP) $(TUXLIBP) $(EXT_LIBP)
 
CCFLGS	=	$(RLFLG) $(CCFLG) $(DEFS) $(TUXDEF) $(DBDEF) $(OSDEF) $(BITDEF) $(INCPS)
CXXFLGS =   $(RLFLG) $(CXXFLG) $(DEFS) $(TUXDEF) $(DBDEF) $(OSDEF) $(BITDEF) $(INCPS)
DLFLGS	=	$(DLFLG) $(LIBPS)
EXFLGS	=	$(EXFLG) $(LIBPS)
 
DLTARG	= $(LIBP)/lib$(TARGET)$(DLFIX)
SLTARG	= $(LIBP)/lib$(TARGET)$(SLFIX)
EXTARG	= $(BINP)/$(TARGET)
#TUXTARG	= $(TUXBINP)/$(TARGET).svr
TUXTARG	= $(BINP)/$(TARGET).svr
#TUXTARGCLI	= $(TUXCLIBINP)/$(TARGET).cli
TUXTARGCLI	= $(BINP)/$(TARGET).cli

debugdynamic: setdebug $(DLTARG)
releasedynamic: setrelease $(DLTARG)
debugstatic: setdebug $(SLTARG)
releasestatic: setrelease $(SLTARG)
debugexec: setdebug $(EXTARG)
releaseexec : setrelease $(EXTARG)
debugtuxsvrexec: setdebug $(TUXTARG)
releasetuxsvrexec : setrelease $(TUXTARG)
#add by mgzhang
debugtuxcliexec: setdebug $(TUXTARGCLI)
#add by tyxiang 20110426
releasetuxcliexec: setrelease $(TUXTARGCLI)

javaclasses : $(OBJS)
	$(ECHO) "Finish java package [$(TARGET)]"
rmi :
	$(ECHO) "rmic [$(TARGET)] ..."
	@$(CD) `dirname $(TARGET)`; \
	@$(RMIC) `basename $(TARGET)`

setdebug:
	$(ECHO) $(DBFLG) > $(MKHOME)/mak/.mak.tmp

setrelease:
	$(ECHO) $(OPFLG) > $(MKHOME)/mak/.mak.tmp

$(SLTARG): $(OBJS) 
	$(ECHO) "Linking   [$(SLTARG)] ..."
	@$(SLINK) $(SLFLG) $@ $(OBJS)
	@$(RM) $(MKHOME)/mak/.mak.tmp

$(DLTARG): $(OBJS) 
	$(ECHO) "Linking  .so [$(DLTARG)] ..."
	@if [ $(OSTYPE) = aix ]; \
	then \
	    if [ -n "$$VPATH" ]; then \
    	    for i in $(OBJS); do \
    	        if [ -a "$$VPATH/$$i" ];  \
    	        then    \
    	            REAL_OBJS="$$REAL_OBJS $$VPATH/$$i"; \
    	        else    \
    	            REAL_OBJS="$$REAL_OBJS $$i";    \
    	        fi;  \
    	    done;    \
    	    echo "$(DLINK) -o $@ $(DLFLGS) $${REAL_OBJS} $(LIBS) $(UXLIBS)"; \
    	    $(DLINK) -o $@ $(DLFLGS) $${REAL_OBJS} $(LIBS) $(UXLIBS); \
    	else \
		    echo "$(DLINK) -o $@ $(DLFLGS) $(OBJS) $(LIBS) $(UXLIBS)"; \
		    $(DLINK) -o $@ $(DLFLGS) $(OBJS) $(LIBS) $(UXLIBS); \
		fi;\
	else \
		$(DLINK) -o `basename $@` $(DLFLGS) $(OBJS) $(LIBS); \
		$(MV) `basename $@` `dirname $@`; \
	fi
	@$(RM) $(MKHOME)/mak/.mak.tmp

$(EXTARG): $(OBJS)
	$(ECHO) "Linking   [$(EXTARG)] ..."
	if [ -n "$$VPATH" ]; then \
    	for i in $(OBJS); do \
            if [ -a "$$VPATH/$$i" ];  \
            then    \
                REAL_OBJS="$$REAL_OBJS $$VPATH/$$i"; \
            else    \
                REAL_OBJS="$$REAL_OBJS $$i";    \
            fi;  \
        done;    \
        $(CC) -o $@ $(EXFLGS) $$REAL_OBJS $(LIBS); \
    else \
	    $(CC) -o $@ $(EXFLGS) $(OBJS) $(LIBS); \
	fi; 
	@$(RM) $(MKHOME)/mak/.mak.tmp

$(TUXTARG): $(OBJS)
	$(ECHO) "buildserver [$(TUXTARG)] ..."
	TUXDIR="$(TUXDIR)" \
	CFLAGS="$(EXFLGS)" \
	$(TUXBS) -o $@ -f "$(OBJS)" -v $(TUXSVCS) -f "$(LIBS)" 
	@#-r $(TUXRSMN)
	$(MV) $(TUXTARG) $(BINP)/$(TARGET)

#add by mgzhang 20061121
$(TUXTARGCLI): $(OBJS)
	$(ECHO) "buildcli [$(TUXTARGCLI)] ..."
	TUXDIR="$(TUXDIR)" \
	CFLAGS="$(EXFLGS)" \
	$(TUXBL) -o $@ -v -f "$(OBJS)" -f "$(LIBS)"
	$(MV) $(TUXTARGCLI) $(BINP)/$(TARGET)


# implicit
 
.SUFFIXES:
.SUFFIXES: .java .class .xc .ec .pc .sqc .c .o
 
.xc.o:
	$(ECHO) "Compiling [$@] ..."
	@$(RM) $*$(DBFIX)
	@$(CP) $*.xc $*$(DBFIX)
	@if [ $(DBTYPE) = ora ]; \
	then \
		$(PC) define=$(DBD) include=$(TUXINCP) include=$(INCP) include=$(JINCP) include=$(JMDINCP) code=ANSI_C iname=$*$(DBFIX); \
		$(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c $*.c; \
		$(RM) $*.lis; \
	else \
		$(EC) -e -ED$(DBD) $(INCPS) $*$(DBFIX); \
		$(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c `basename $*`.c; \
		$(RM) `basename $*`.c; \
	fi
	@$(RM) $*$(DBFIX) $*.c

.pc.o:
	$(ECHO) "Compiling [$@] ..."
	$(PC) define=$(DBD) include=$(TUXINCP) include=$(INCP) include=$(JINCP) include=$(JMDINCP) code=ANSI_C iname=$*$(DBFIX)
	$(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c $*.c
	$(RM) $*.lis $*.c

.ec.o:
	$(ECHO) "Compiling [$@] ..."
	$(EC) -e -ED$(DBD) $(INCPS) $*$(DBFIX); \
	$(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c `basename $*`.c; \
	$(RM) `basename $*`.c; \

.sqc.o:
	$(ECHO) "Compiling [$@] ..."
	@$(RM) $*.bnd
	@$(DB2) connect to $(DBNM)
	@$(DB2) prep `basename $<` bindfile
	@$(DB2) bind `basename $*`.bnd
	@$(DB2) connect reset
	@$(DB2) terminate
#	$(MV) `basename $*`.bnd $(BNDDIR)
	@PREFIX=`basename $* | cut -c 1-2 `; \
	BND=`basename $*`.bnd; \
	if [ "$$PREFIX" = "MG" ]; then \
	    $(MV) $$BND $(BNDDIR)/mgm; \
	elif [ "$$PREFIX" = "ON" ]; then\
	    $(MV) $$BND $(BNDDIR)/onl; \
	elif [ "$$PREFIX" = "BA" ]; then\
	    $(MV) $$BND $(BNDDIR)/bat; \
	elif [ "$$PREFIX" = "GF" ]; then\
	    $(MV) $$BND $(BNDDIR)/gfs; \
	elif [ "$$PREFIX" = "GL" ]; then\
	    $(MV) $$BND $(BNDDIR)/glb; \
	else \
	    $(MV) $$BND $(BNDDIR); \
	fi
#   Sort bnd files into different directory. added by tyxiang 2011-3-23
	@$(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c `basename $*`.c
#	@$(RM) $*.c
	
.cpp.o:
	$(ECHO) "Compiling [$@] ..."
	@$(CD) `dirname $@` ; \
	$(CXX) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CXXFLGS) -c `basename $<`

.c.o:
	$(ECHO) "Compiling [$@] ..."
	$(CD) `dirname $@`; \
	$(CC) -o $@ `cat $(MKHOME)/mak/.mak.tmp` $(CCFLGS) -c `basename $<`
 
.java.class:
	$(ECHO) "Compiling [$@] ..."
	@$(CD) `dirname $@`; \
	$(JAVAC) `basename $<`

# clearing object codes
 
clean: cleanup
	$(ECHO) "Cleaning up ..."
	@$(RM) $(SLTARG) $(DLTARG) $(EXTARG) $(TUXTARG) $(TUXTARGCLI)
	@if [ -n "$(OBJDIR)" ]; then    \
	    for i in $(OBJS); do       \
	        $(RM) $(OBJDIR)/$$i;   \
	    done;                      \
    fi;
cleanup:
	@$(RM) $(OBJS)
